//
//  ECScanViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/3/23.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECScanViewController.h"
#import "ReaderDocument.h"
#import "ReaderViewController.h"
#import "ECQRShowViewController.h"

@interface ECScanViewController ()<ReaderViewControllerDelegate>

@end

@implementation ECScanViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫一扫";
}

#pragma mark -扫码结果处理

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array{
    LBXScanResult *result = array[0];
    DLog(@"%@",result.strScanned);
    
    ECQRShowViewController *vc = [[ECQRShowViewController alloc]init];
    vc.url = result.strScanned;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array{
//    LBXScanResult *result = array[0];
//    DLog(@"%@",result.strScanned);
//
//    NSString *urlStr = [NSString stringWithFormat:@"http://edu6.sdkhcm.com/api/qrcode?QRcode=%@",result.strScanned];
//    [HWHttpTool requestWithMethod:@"GET"
//                              url:urlStr
//                           params:nil
//                          headers:nil
//                          success:^(id json) {
//                              if ([json[@"code"] intValue] == 200) {
//                                  NSString *type = json[@"data"][@"type"];
//                                  NSString *resourceUrlStr = json[@"data"][@"value"];
//                                  if ([type isEqualToString:@"pdf"]) {//一对一之下的pdf
//                                      [self requestPDFStr:resourceUrlStr];
//                                  }else if ([type isEqualToString:@"video"]){//一对一之下的video
//
//                                  }else if ([type isEqualToString:@"audio"]){//一对一之下的audio
//
//                                  }else if ([type isEqualToString:@"course"]){//一对多之下的课程
//
//                                  }
//                              }else{
//                                  [self reStartDevice];
//                                  [MBProgressHUD showTipMessageInView:json[@"message"]];
//                              }
//                          }
//                          failure:^(NSError *error) {
//                              [self reStartDevice];
//                              [MBProgressHUD showTipMessageInView:@"网络连接错误"];
//                          }];
//}

///PDF 先下载保存到本地 在打开
-(void)requestPDFStr:(NSString *)str{
    [HWHttpTool downloadTaskWithURL:str completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSString *fileStr = [NSString stringWithFormat:@"%@",filePath];
        ReaderDocument *doc = [[ReaderDocument alloc] initWithFilePath:[fileStr substringFromIndex:7] password:nil];
        ReaderViewController *rvc = [[ReaderViewController alloc] initWithReaderDocument:doc];
        rvc.delegate = self;
        [self presentViewController:rvc animated:YES completion:nil];
    }];
}

#pragma mark - ReaderViewControllerDelegate
- (void)dismissReaderViewController:(ReaderViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
